package com.android.versiondownloadlibrary;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;

/**
 * Created by $Liudan on 2018/5/16 0016.
 */
public class DownloadBuilder {

    public static DownloadBuilder getInstance() {
        return AllenVersionCheckerHolder.allenVersionChecker;
    }

    private static class AllenVersionCheckerHolder {
        public static final DownloadBuilder allenVersionChecker = new DownloadBuilder();
    }

    public static void updateVersion(Context context, String url,int update_mode) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            try {
                throw new SecurityException(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            try {
                throw new SecurityException(Manifest.permission.READ_EXTERNAL_STORAGE);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
        Intent intent = new Intent(context, VersionService.class);
        intent.putExtra("url", url);
        intent.putExtra("update_mode",update_mode);
        context.startService(intent);
    }

}
